/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:SysMessageServiceImpl.java
 * Version:1.0
 * Date:2011-12-7
 * Description: 
 * author:zhijian.zhang
 */
package com.tietiao.service.sysmessage;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tietiao.common.util.PageModel;
import com.tietiao.dao.SysMessageMapper;
import com.tietiao.dao.model.MidMessageUser;
import com.tietiao.dao.model.SysMessage;
import com.tietiao.dao.model.SysMessageCriteria;
import com.tietiao.service.bean.SysMessageBean;
import com.tietiao.service.midmessageuser.MidMessageUserService;

@Service
public class SysMessageServiceImpl implements SysMessageService {

	@Resource
	private SysMessageMapper sysMessageMapper;
	@Resource
	private MidMessageUserService midMessageUserService;
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.sysmessage.SysMessageService#addSysMessage(com.tietiao
	 * .dao.model.SysMessage)
	 */
	@Override
	public int addSysMessage(SysMessage sysMessage) {
		sysMessageMapper.insert(sysMessage);
		// 维护MID_MESSAGE_USER业务表逻辑
		return sysMessage.getMessageId();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tietiao.service.sysmessage.SysMessageService#getSysMessagePagesByUserId
	 * (int, java.lang.Integer, int, int)
	 */
	@Override
	public PageModel<SysMessage> getSysMessagePagesByUserId(int userId,
			Integer msgIsValid, int pageNo, int pageSize) {

		SysMessageCriteria sysMessageCriteria = new SysMessageCriteria();
	
		sysMessageCriteria.setOrderByClause("CREATE_TIME DESC, IS_VALID DESC");
		
		sysMessageCriteria.setStart((pageNo - 1) * pageSize);
		sysMessageCriteria.setLimit(pageSize);
		
		SysMessageCriteria.Criteria criteria = sysMessageCriteria
				.createCriteria().andUserIdEqualTo(userId);
		if (msgIsValid != null) {
			criteria.andIsValidEqualTo(msgIsValid);
		}
		List<SysMessage> sysMessages = sysMessageMapper
				.selectByExample(sysMessageCriteria);
		int count = sysMessageMapper.countByExample(sysMessageCriteria);
		PageModel<SysMessage> result = new PageModel<SysMessage>(sysMessages,
				pageNo, pageSize, count);

		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tietiao.service.sysmessage.SysMessageService#getSysMessages(int,
	 * java.lang.Integer)
	 */
	@Override
	public List<SysMessage> getSysMessages(SysMessageBean messageBean) {
		
		List<SysMessage> sysMessages = sysMessageMapper.selectMessage(messageBean);
		return sysMessages;
	}

	@Override
	public List<SysMessage> getUnReadGroupCount(SysMessageBean messageBean) {
		return sysMessageMapper.getUnReadGroupCount(messageBean);
	}

	@Override
	public int updateMessageRead(SysMessageBean messageBean) {
		return sysMessageMapper.updateMessageRead(messageBean);
	}

	/* (non-Javadoc)
	 * @see com.tietiao.service.sysmessage.SysMessageService#addSysMessage(com.tietiao.dao.model.SysMessage, java.lang.Integer[])
	 */
	@Override
	public int addSysMessage(SysMessage sysMessage, String[] receiveIds) {
		int result = addSysMessage(sysMessage);
		if(result>0 && receiveIds!=null){
			for (String receiveId : receiveIds) {
				MidMessageUser middSysMessage = new MidMessageUser();
				middSysMessage.setIsView(0);
				middSysMessage.setMessageId(sysMessage.getMessageId());
				middSysMessage.setUserId(Integer.parseInt(receiveId));
				midMessageUserService.insertMidMessageUser(middSysMessage);
			}
		}
		return result;
	}

}
